import java.nio.file.Path;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Convert {
    public static String convertApiOperation(String line,Path filePath, Map<String, String> props) {
        Matcher matcher = Patterns.PATTERN_BLANK.matcher(line);
        String bash = "";
        if (matcher.find()) {
            bash = matcher.group();
        }
        validProps(filePath, "ApiOperation", props, "value", "httpMethod", "notes");

        StringBuilder str = new StringBuilder(bash).append("@Operation(");
        for (String key : props.keySet()) {
            switch (key) {
                case "value":
                    str.append("summary = \"").append(props.getOrDefault(key, "")).append("\"");
                    break;
                case "httpMethod":
                    str.append("method = \"").append(props.getOrDefault(key, "")).append("\"");
                    break;
                case "notes":
                    str.append("description = \"").append(props.getOrDefault(key, "")).append("\"");
                    break;
                default:
                    break;
            }
            str.append(", ");
        }
        str.delete(str.length() - 2, str.length() - 1);
        str.append(")");
        return str.toString();
    }

    public static String convertApiModel(Path filePath, Map<String, String> props) {
        validProps(filePath, "ApiModel", props, "value", "description", "discriminator");
        StringBuilder str = new StringBuilder("@Schema(");
        for (String key : props.keySet()) {
            switch (key) {
                case "value":
                    str.append("title = \"").append(props.getOrDefault(key, "")).append("\"");
                    break;
                case "description":
                    str.append("description = \"").append(props.getOrDefault(key, "")).append("\"");
                    break;
                case "discriminator":
                    str.append("title = \"").append(props.getOrDefault(key, "")).append("\"");
                    break;
                default:
                    break;
            }
            str.append(", ");
        }
        str.delete(str.length() - 2, str.length() - 1);
        str.append(")");
        return str.toString();
    }

    public static String convertApiModelProperty(String line, Path filePath, Map<String, String> props) {
        Matcher matcher = Patterns.PATTERN_BLANK.matcher(line);
        String bash = "";
        if (matcher.find()) {
            bash = matcher.group();
        }
        validProps(filePath, "ApiModelProperty", props, "required", "value", "notes", "example", "hidden", "allowableValues", "dataType", "name");

        StringBuilder str = new StringBuilder(bash).append("@Schema(");
        for (String key : props.keySet()) {
            switch (key) {
                case "value":
                    str.append("title = \"").append(props.getOrDefault(key, "")).append("\"");
                    break;
                case "required":
                    str.append("required = ").append(props.getOrDefault(key, "false")).append("");
                    break;
                case "notes":
                    str.append("description = \"").append(props.getOrDefault(key, "")).append("\"");
                    break;
                case "example":
                    str.append("example = \"").append(props.getOrDefault(key, "")).append("\"");
                    break;
                case "hidden":
                    str.append("hidden = ").append(props.getOrDefault(key, "false")).append("");
                    break;
                case "dataType":
                    str.append("type = \"").append(props.getOrDefault(key, "")).append("\"");
                    break;
                case "name":
                    str.append("name = \"").append(props.getOrDefault(key, "")).append("\"");
                    break;
                case "allowableValues":
                    str.append("allowableValues = \"").append(props.getOrDefault(key, "")).append("\"");
                    break;
                default:
                    break;
            }
            str.append(", ");
        }
        str.delete(str.length() - 2, str.length() - 1);
        str.append(")");
        return str.toString();
    }

    public static void validProps(Path filePath, String tag, Map<String, String> props, String... allowKeys) {
        if (Objects.nonNull(props) && props.size() > 0) {
            for (String key : props.keySet()) {
                boolean has = false;
                for (int i = 0; i < allowKeys.length; i++) {
                    if (allowKeys[i].equals(key)) {
                        has = true;
                        break;
                    }
                }
                if (!has) {
                    System.out.println(props + "\t" + Arrays.toString(allowKeys));
                    throw new RuntimeException(String.format("标签:[%s]\t属性:[%s] 无法处理 文件路径:[%s]", tag, key, filePath));
                }
            }
        }
    }
}
